#include "action.h"
#include <cmath>

action::action(actor* act): starttime( 0 ),
                            endtime( 0 ),
                            type( AC_TY_NONE ),
                            myAct( act )
{}
action::~action(){}

action_t action::getType(){ return type; }
void action::setType(action_t ty){
    type = ty;
}

clock_t action::getSTime(){ return starttime; }
clock_t action::getETime(){ return endtime; }
void action::setSTime(clock_t st){
    if( st == 0 ) starttime = clock();
    else starttime = st;
}
void action::setETime(clock_t et){
    endtime = et;
}
void action::setDuration(clock_t dur){
    endtime = starttime + dur;
}

float action::getProgress(){
    clock_t cl;
    if( (cl = clock()) >= endtime ) return 1;
    return ((float)(cl - starttime))/((float)(endtime - starttime));
}

action_idle::action_idle(actor* act):   action( act )
{
    type = AC_TY_IDLE;
}
action_idle::~action_idle(){}

action_move::action_move(actor* act,coord_t destination,float dist,unsigned int speed):
        action( act ),
        origin( act->getCoord() ),
        dest( destination )
{
    type = AC_TY_MOVE;
    setSTime();
    setDuration((clock_t)((dist*1000000)/(float)speed));
}
action_move::~action_move(){}

coord_t action_move::getCurPos(){
    float progress = getProgress();
    coord_t out;
    out.X_coord = (unsigned int)((float)dest.X_coord*progress + (float)origin.X_coord*(1-progress));
    out.Y_coord = (unsigned int)((float)dest.Y_coord*progress + (float)origin.Y_coord*(1-progress));
    return out;
}
coord_t action_move::getOrig(){
    return origin;
}
coord_t action_move::getDest(){
    return dest;
}
